﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class CSInsertArray : SpeechCommand
    {
        private string[] types = null;

        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            if (context == null)
            {
                types = new string[] { };
                return null;
            }
            List<string> spokenTypes = new List<string>();

            Choices symbolChoices = new Choices();
            int nChoices = 0;
            foreach (var s in context.Symbols)
            {
                if (s.Category == SymbolCategory.Type && s.Parameters == 0)
                {
                    symbolChoices.Add(new SemanticResultValue(s.SpokenForm, s.WrittenForm + "[] "));
                    spokenTypes.Add(s.SpokenForm);
                    nChoices++;
                }
            }
            foreach (var lib in libraries)
            {
                foreach (var s in lib.Symbols)
                {
                    if (s.Category == SymbolCategory.Type && s.Parameters == 0)
                    {
                        symbolChoices.Add(new SemanticResultValue(s.SpokenForm, s.WrittenForm + "[] "));
                        spokenTypes.Add(s.SpokenForm);
                        nChoices++;
                    }
                }

            }
            types = spokenTypes.ToArray();
            if (nChoices != 0)
            {
                GrammarBuilder gb = new GrammarBuilder();
                gb.Append(new SemanticResultKey("WrittenForm", symbolChoices));
                gb.Append("array");
                return gb;
            }
            else
                return null;
        }

        public override string Name
        {
            get { return "Insert array"; }
        }

        public override string Description
        {
            get { return Name; }
        }

        public override string GetAlternativesFeedback(string recognizedText, SemanticValue semValue)
        {
            return (string)semValue["WrittenForm"].Value;
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            return types.Contains<string>(spokenForm);
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            string textToType = (string)semValue["WrittenForm"].Value;
            callingMode.CurrentOutputHandler.SendKeys(textToType);
            callingMode.CurrentGuiHandler.SetPrimaryFeedback(textToType);
            return "^z^z";
        }
    }
}
